<?php

/* this is an unfinished start to a variant on image.php, which can draw squares per team, not merely per league. image_cont.php is another try. */

header("Content-type: image/png");

require_once("database.inc.php");
require_once("track.inc.php");

if (!isset($_GET['nolog'])) hit();

if (!is_numeric($_GET['c']) || !is_numeric($_GET['l'])) die;

$new = 0;
if (isset($_GET['n']) && $_GET['n'] == "1") $new = 1;

$histo = 0;
if (isset($_GET['h']) && $_GET['h'] == "1") $histo = 1;

$date = "2006-02-03";
$must_do = "create table promotions_29 as SELECT d1.teamid teamid, d1.leagueid leagueid, s1.seriesid s1, s2.seriesid s2, s1.level l1, s2.level l2 FROM `teams_2006-04-26` d1, series_new s1 LEFT JOIN `teams_2006-04-03` d2, series_old s2 ON d1.teamid = d2.teamid WHERE d1.seriesid = s1.seriesid AND d2.seriesid = s2.seriesid";

$leagueid = $_GET['c'];
$level = $_GET['l'];

$serienames = array();
$dbh = connect_db("dbm");

$seriestable = "series";
if ($new == 1) {
	$seriestable = "series_new";
} else {
	$seriestable = "series_old";
}

$query = mysql_query("SELECT * FROM $seriestable WHERE CountryID = $leagueid AND level = $level ORDER BY level ASC, seriesid ASC", $dbh);
while ($row = mysql_fetch_array($query)) {
	$serienames[$row['SeriesID']] = $row['SeriesName'];
}

$condition = "userid != 0";

if (isset($_GET['sup']) && $_GET['sup'] == 1) {
	$condition = "hassupporter = 1";
}


$querytext = "SELECT s1, l2 - l1 diff from promotions_29 where leagueid = $leagueid and l1 = $level order by s1 desc";

if (isset($_GET['act']) && $_GET['act'] == 1) {
	$querytext = "SELECT seriesid, 8 - ROUND(AVG(UNIX_TIMESTAMP( FetchedDate ) - UNIX_TIMESTAMP( LastLoginDate )) / (86400*7)) num FROM `teams_$date` WHERE $condition AND leagueid = $leagueid GROUP BY seriesid";
}

if (isset($_GET['age']) && $_GET['age'] == 1) {
	$subquery = mysql_query("SELECT UNIX_TIMESTAMP(MAX(signupdate)) oldest FROM `teams_$date` WHERE leagueid = $leagueid");
	while($row = mysql_fetch_array($subquery)) {
		$oldest = $row['oldest'];	
	}
	$query = "SELECT seriesid, (something with $oldest) num FROM `teams_$date` WHERE $condition AND leagueid = $leagueid GROUP BY seriesid";
}

$query = mysql_query("$querytext", $dbh);
while ($row = mysql_fetch_array($query)) {
	if (is_null($row['diff'])) $wos[$row['s1']][] = $row['diff'] = "null";
	else $wos[$row['s1']][] = $row['diff'];
}

if ($_GET['b'] > 0) {
	$blocksize = $_GET['b'];
} else {
	$blocksize = 8;	
}

$vertical_multiplier = 0.25;
$horizontal_multiplier = 0.5;
$fillgrid = 0;

if ($level < 6) {
	$bpl = pow(2,$level)/2;
} else {
	if ($new == 1) {
		if ($level <= 7) {
			$bpl = 32;
		} else if ($level <= 9) {
			$bpl = 32;
			$horizontal_multiplier = 0.25;
		} else if ($level <= 11) {
			$bpl = 64;
			$horizontal_multiplier = 0.25;
			$vertical_multiplier = 0.125;
		} else if ($level <= 13) {
			$bpl = 64;
			$horizontal_multiplier = 0.125;
			$vertical_multiplier = 0.125;
		} else {
			die ("no system defined for 14-level leagues and bigger...\n");
		}
	} else {
		$bpl = 32;
	}
}

//if $histo is 1, add margins and space for the histogram
if ($histo == 1) 
	$im = imagecreatetruecolor(2*$bpl*$blocksize, $bpl*$blocksize);
else
	$im = imagecreatetruecolor($bpl*$blocksize, $bpl*$blocksize);

$bg		= imagecolorallocate($im,127,127,127);

$white		= imagecolorallocate($im,255,255,255);
$grey		= imagecolorallocate($im,232,232,232);
$pink		= imagecolorallocate($im,255,0  ,255);
$red		= imagecolorallocate($im,255,0  ,0  );
$orange		= imagecolorallocate($im,255,144,0  );
$yellow		= imagecolorallocate($im,255,255,0  );
$cyan		= imagecolorallocate($im,0  ,192,192);
$green		= imagecolorallocate($im,0  ,160,0  );
$blue		= imagecolorallocate($im,0  ,0  ,192);
$black		= imagecolorallocate($im,0  ,0  ,0  );

$colors["null"] = imagecolorallocate($im,0  ,0  ,0  );
$colors["-10"]	= imagecolorallocate($im,128,255,0  );
$colors["-9"]	= imagecolorallocate($im,192,255,0  );
$colors["-8"]	= imagecolorallocate($im,255,255,0  );
$colors["-7"]	= imagecolorallocate($im,255,192,0  );
$colors["-6"]	= imagecolorallocate($im,255,128,0  );
$colors["-5"]	= imagecolorallocate($im,255,64 ,0  );
$colors["-4"]	= imagecolorallocate($im,255,0  ,0  );
$colors["-3"]	= imagecolorallocate($im,255,64 ,64 );
$colors["-2"]	= imagecolorallocate($im,255,128,128);
$colors["-1"]	= imagecolorallocate($im,255,192,192);
$colors["0"]	= imagecolorallocate($im,255,255,255);
$colors["1"]	= imagecolorallocate($im,192,192,255);
$colors["2"]	= imagecolorallocate($im,128,128,255);
$colors["3"]	= imagecolorallocate($im,64 ,64 ,255);
$colors["4"]	= imagecolorallocate($im,0  ,0  ,255);
$colors["5"]	= imagecolorallocate($im,0  ,255,255);


imagefill($im,0,0,$bg);

$px = 0;
$py = 0;


$bnum = 0;
foreach(array_keys($serienames) as $seriesid) {
	$bnum++;
	for ($i = 0; $i <= 7; $i++) {
		teamdraw($im, $px, $py, $blocksize, $horizontal_multiplier, $vertical_multiplier, $i, $wos[$seriesid][$i], $colors);
	}
	if ($blocksize >= 32) {
		imagecenteredstring($im, 1, $px, $px+0.5*$horizontal_multiplier*$blocksize-2, $py+0.5*$vertical_multiplier*$blocksize-3, utf8_decode($serienames[$seriesid]), $white);
	}
	if (2*$horizontal_multiplier*$bnum==$bpl) {
		$bnum = 0;
		$px = 0;
		$py += 4*$vertical_multiplier*$blocksize;
	} else {
		$px += 2*$horizontal_multiplier*$blocksize;
	}
}

if ($histo == 1) {
	imagefilledrectangle($im,$bpl*$blocksize,0,2*$bpl*$blocksize - 1,2*$bpl*$blocksize - 1,$white);
	imageline($im,$bpl*$blocksize + 10, 0, $bpl*$blocksize + 10, $bpl*$blocksize - 1, $black);
}

imagepng($im);
imagedestroy($im);

function imagecenteredstring ( &$img, $font, $xMin, $xMax, $y, $str, $col ) {
	$textWidth = imagefontwidth( $font ) * strlen( $str );
	$xLoc = ( $xMax - $xMin - $textWidth ) / 2 + $xMin + $font;
	imagestring( $img, $font, $xLoc, $y, $str, $col );
}

function teamdraw ( &$im, $px, $py, $blocksize, $horizontal_multiplier, $vertical_multiplier, $i, $diff, &$colors) {
	$hoffset = $blocksize * $horizontal_multiplier * ($i % 2);
	$voffset = $blocksize * $vertical_multiplier * floor($i / 2);
	imagefilledrectangle($im, $px+$hoffset, $py+$voffset, $px+$hoffset+($horizontal_multiplier*$blocksize)-1, $py+$voffset+($vertical_multiplier*$blocksize)-1, $colors["$diff"]);
	}
?> 
